package ca.uwaterloo.mylongcall;

import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	LinearLayout mainlayout;
	
	public Button addButtonForCard(String pre, String pin){	////adds a button for this card on the main screen
		CallCard card = new CallCard(pre, pin);
		Button callcard = new Button(getApplicationContext());
		callcard.setText(card.PIN);
		callcard.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		return callcard;
	}
	
	public Button addButtonForContact(String name, String phone){	//adds a button for this guy on the main screen
		Contact guy = new Contact(name, phone);
		Button contact = new Button(getApplicationContext());
		contact.setText(guy.Name);
		contact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {	//Dial this contact using DefaultCallCard.DialNum to start
				//String ToBeDialed = 
				final Intent dial = new Intent(Intent.ACTION_CALL);
				dial.setData(Uri.parse("tel:1281903818312890389081290132"));
				//dial.setData(Uri.parse(ToBeDialed));
				startActivity(dial);
			}
		});
		return contact;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		SP spmanager = new SP();
		
		CallCard.total = spmanager.getCallCardnumber();
		CallCard.inuse = spmanager.getDefaultCallCard();
		
		//for test use.
		Set<String> fortest;
		
		while (CallCard.total==0) {
			// if there is no default call card information stored in the 
			//shared preference, which means it doesn't has a call card.
			//it should now go to the ManageCardActivity, and make sure 
			//the user register a call card.
			
			//But here I just do this in the code.
			spmanager.putin("Default_Pin", "1234567");
			spmanager.putin("1234567_PRE", "18669138555,,1,");
			fortest = null;
			fortest.add("1234567");
			spmanager.putin("Pin_Set",fortest);
			CallCard.total = spmanager.getCallCardnumber();
		}
		
		//the code below is only for test use.
		fortest =null;
		fortest.add("a");
		fortest.add("b");
		spmanager.putin("Name_Set",fortest);
		spmanager.putin("a","85405555");
		spmanager.putin("b","85410555");
		
		Contact.total = spmanager.getContactnumber();
		Contact.all= spmanager.getAllContacts();
		
		setContentView(R.layout.activity_main);
		
		for (int i=0; i<Contact.total; i++) {
			Contact.all[i].present();
		}
		
		mainlayout = (LinearLayout) findViewById(R.id.layout1);
		mainlayout.setOrientation(LinearLayout.VERTICAL);
		
		TextView mycontacts = new TextView(getApplicationContext());
		mycontacts.setText("My Contacts");
		
		Button managecallcards = (Button) findViewById(R.id.managecallcards);
		Button addcontacts = (Button) findViewById(R.id.addcontact);
		managecallcards.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent manager = new Intent(MainActivity.this, ManageCardsActivity.class);
				startActivity(manager);
			}
		});
		
		mainlayout.addView(mycontacts);
		//testing, testing...
		mainlayout.addView(addButtonForContact("God", "514"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		mainlayout.addView(addButtonForContact("Dog", "5555"));
		//mainlayout.addView(addButtonForCard("23321776", "754789"));
		//testing complete!

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
